﻿package
{
	import com.greensock.TweenLite;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	import tools.events.InteractiveEvent;
	import flash.text.TextField;
	import flash.display.MovieClip;
	import tools.utils.Rnd;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.events.Event;
	import flash.display.Sprite;
	
	public class Main extends MovieClip
	{
		private var _timer:Timer;
		private var _timerStep:int = 3000;
		private static const PERSON_MAX_WAIT:int = 60;
		private static const PERSON_PER_FLOOR:int = 9;
		private var _waitPersonCount:int;
		private var _liftBox:LiftBox;
		
		private var _startBtn:MovieClip;
		private var _stopBtn:MovieClip;
		private var _direction:MovieClip;
		private var _currentFTxt:TextField;
		private var _targetFTxt:TextField;
		private var _speedTxt:TextField;
		private var _allPerson:TextField;
		private var _waitPerson:TextField;
		private var _upF1:TextField;
		private var _upF2:TextField;
		private var _upF3:TextField;
		private var _upF4:TextField;
		private var _upF5:TextField;
		private var _upF6:TextField;
		
		private var _downF1:TextField;
		private var _downF2:TextField;
		private var _downF3:TextField;
		private var _downF4:TextField;
		private var _downF5:TextField;
		private var _downF6:TextField;
		
		public function Main()
		{
			addEventListener(Event.ADDED_TO_STAGE, onAdd);
		}
		
		private function onAdd(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAdd);
			initView();
			initValue();
			
			//_liftBox.onMoveCom(1);
		}
		
		private function initValue():void
		{
			_timer = new Timer(_timerStep);
			_timer.addEventListener(TimerEvent.TIMER, onTimer);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerCom);
			
			_startBtn.addEventListener(MouseEvent.CLICK, onStart);
			_stopBtn.addEventListener(MouseEvent.CLICK, onStop);
			_liftBox.addEventListener(LiftEvent.GO_IN_LIFT, onGoInLift);
			_liftBox.addEventListener(LiftEvent.CHANGE_MOVE_TYPE, onChangeMoveType);
			_liftBox.addEventListener(LiftEvent.GO_OUT_LIFT, onGoOutLift);
			_liftBox.addEventListener(LiftEvent.CHANGE_CURRENT_FLOOR, onChangeCurrentFlorr);
			_liftBox.addEventListener(LiftEvent.CHANGE_TARGET_FLOOR, onChangeTargetFloor);
		}
		
		private function onChangeTargetFloor(event:InteractiveEvent):void 
		{
			_targetFTxt.text = String(event.data);
		}
		
		private function onChangeCurrentFlorr(event:InteractiveEvent):void 
		{
			_currentFTxt.text = String(event.data);
		}
		
		private function onStop(event:MouseEvent):void
		{
			_timer.stop();
		}
		
		private function onStart(event:MouseEvent):void
		{
			//初始状态人员
			//addPersonfloor(1, 3);
			//addPersonfloor(1, 2);
			//addPersonfloor(1, 5);
			//
			//addPersonfloor(2, 3);
			//addPersonfloor(2, 4);
			//addPersonfloor(2, 1);
			//
			//addPersonfloor(3, 2);
			//
			//addPersonfloor(4, 5);
			//addPersonfloor(4, 2);
			//
			//addPersonfloor(5, 3);
			//addPersonfloor(5, 4);
			//
			//addPersonfloor(6, 3);
			//addPersonfloor(6, 1);
			//addPersonfloor(6, 4);
			
			
			
			
			
			_timer.start();
			setTimeout(_liftBox.onMoveCom, 10000, 1);
		}
		
		private function addPersonfloor(from:int, to:int):void
		{
			var personItem:PersonItem = new PersonItem();
			personItem.fromFloor = from;
			personItem.toFloor = to;
			addFloorPerson(personItem);
		}
		
		private function onChangeMoveType(event:InteractiveEvent):void
		{
			_direction.gotoAndStop(event.data);
		}
		
		private function onGoInLift(event:InteractiveEvent):void
		{
			var person:PersonItem = event.data as PersonItem;
			for (var i:int = 0; i < this["floor"+person.fromFloor].numChildren; i++) 
			{
				var floorPerson:FloorPerson = this["floor" + person.fromFloor].getChildAt(i) as FloorPerson;
				if (floorPerson.personItem.fromFloor == person.fromFloor&&floorPerson.personItem.toFloor == person.toFloor) {
					TweenLite.to(floorPerson, 0.25, { alpha:0, yoyo:true, onComplete:onTweenGoInLift, onCompleteParams:[floorPerson] } );
					trace("楼层人消失--------------------------------");
				}
			}
			//autoAlign();
			updatePersonNum(person.fromFloor, person.toFloor, -1);
		
		}
		
		private function onTweenGoInLift(floorPerson:FloorPerson):void 
		{
			autoAlign();
			floorPerson.parent.removeChild(floorPerson);
		}
		
		private function onGoOutLift(event:InteractiveEvent):void
		{
			updatePersonNum(0, 0, 0, true);
		}
		
		private function onTimerCom(event:TimerEvent):void
		{
			
		}
		
		private function onTimer(event:TimerEvent):void
		{
			
			var fromFloor:int = getfromFloor();
			if (fromFloor > -1)
			{
				var personItem:PersonItem = new PersonItem();
				personItem.fromFloor = fromFloor;
				personItem.toFloor = getToFloor(fromFloor);
				addFloorPerson(personItem);
			}
			else
			{
				_timer.stop();
			}
		}
		
		/*
		 * 从哪儿来
		 */
		private function getfromFloor():int
		{
			var floor:int = -1;
			var waitPerson:int = 0;
			for (var i:int = 1; i <= 6; i++)
			{
				waitPerson += this["floor" + i].numChildren;
			}
			//达到60人则不在添加
			if (waitPerson >= PERSON_MAX_WAIT)
				return floor;
			while (floor == -1 || this["floor" + floor].numChildren == PERSON_PER_FLOOR)
			{
				floor = Rnd.integer(1, 7);
			}
			return floor;
		}
		
		/*
		 * 到哪儿去
		 */
		private function getToFloor(fromFloor:int):int
		{
			var toFloor:int;
			while (toFloor == 0 || toFloor == fromFloor)
			{
				toFloor = Rnd.integer(1, 7);
			}
			return toFloor;
		}
		
		/*
		 * 向楼层添加人员
		 */
		private function addFloorPerson(personItem:PersonItem):void
		{
			var floorPerson:FloorPerson = new FloorPerson();
			floorPerson.update(personItem);
			var floorIndex:int = personItem.fromFloor;
			this["floor" + floorIndex].addChild(floorPerson);
			floorPerson.x = (PERSON_PER_FLOOR - this["floor" + floorIndex].numChildren) * floorPerson.width;
			floorPerson.y = 90 - floorPerson.height;
			updatePersonNum(personItem.fromFloor, personItem.toFloor, 1);
			_liftBox.addPeople(personItem);
		}
		
		/*
		 * 自动对齐余下的人
		 */
		private function autoAlign():void
		{
			for (var i:int = 1; i <= 6; i++)
			{
				for (var j:int = 0; j < this["floor" + i].numChildren; j++)
				{
					var floorPerson:FloorPerson = this["floor" + i].getChildAt(j) as FloorPerson;
					//trace("初始X：" + floorPerson.x);
					floorPerson.x = (PERSON_PER_FLOOR - j-1) * floorPerson.width;
					//trace("变换后X：" + floorPerson.x);
				}
			}
		}
		
		private function initView():void
		{
			_startBtn = this["startBtn"];
			_stopBtn = this["stopBtn"];
			_currentFTxt = this["currentFtxt"];
			_targetFTxt = this["targetFtxt"];
			_direction = this["direction"];
			_speedTxt = this["speedTxt"];
			_allPerson = this["allPersonTxt"];
			_waitPerson = this["waitPersonTxt"];
			_liftBox = this["liftBox1"];
			_speedTxt.text = _timerStep / 1000 + "s";
		}
		
		private function updatePersonNum(fromFloor:int, toFloor:int, mode:int, deletePerson:Boolean = false):void
		{
			var allNum:int = int(this["allPersonTxt"].text);
			var waitNum:int = int(this["waitPersonTxt"].text);
			
			if (deletePerson)
			{
				this["allPersonTxt"].text = String(allNum - 1);
				return;
			}
			var upNum:int = int(this["upF" + fromFloor].text);
			var downNum:int = int(this["downF" + fromFloor].text);
			if (toFloor > fromFloor)
			{
				TextField(this["upF" + fromFloor]).text = String(upNum + mode * 1);
			}
			else
			{
				TextField(this["downF" + fromFloor]).text = String(downNum + mode * 1);
			}
			
			if(mode != -1) this["allPersonTxt"].text = String(allNum + mode * 1);
			this["waitPersonTxt"].text = String(waitNum + mode * 1);
		}
	}
}
